package progetto3.managerAB;

import sde.semaforo.Semaforo;
import sde.semaforo.SemaforoBinario;

public class ManagerSem extends ManagerAB {

	private Semaforo mutex = new SemaforoBinario(1);
	private Semaforo wait = new SemaforoBinario(0);

	@Override
	public void request(MyRunnable r) {
		mutex.P();
		if(!canPass(r)){
			w++;
			mutex.V();
			wait.P();
			w--;
		}
		entered(r);
		mutex.V();
	}

	@Override
	public void release(MyRunnable r) {
		mutex.P();
		exited(r);
		if((cnt[(r.getId()+1)%DIM]<2 || cntp==MAXP) && w>0)
			wait.V();//pass-the-baton
		else
			mutex.V();
	}
}
